package com.mayikt.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mayikt.base.BaseApiService;
import com.mayikt.base.BaseResponse;
import com.mayikt.dto.req.UserReqRegisterDTO;
import com.mayikt.entity.UserDO;
import com.mayikt.mapper.UserMapper;
import com.mayikt.service.MemberRegisterService;
import com.mayikt.utils.MD5Util;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Name MemberRegisterServiceImpl
 * @Author 枫火
 * @Date 2021/4/21 16:35
 * @Description
 **/
@RestController
public class MemberRegisterServiceImpl extends BaseApiService implements MemberRegisterService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public BaseResponse<JSONObject> register(UserReqRegisterDTO userReqRegisterDto) {
        // 参数验证
        String mobile = userReqRegisterDto.getMobile();
        if (StringUtils.isEmpty(mobile)) {
            return setResultError("mobile参数不能为空");
        }
        String password = userReqRegisterDto.getPassword();
        if (StringUtils.isEmpty(password)) {
            return setResultError("password参数不能为空");
        }
        // 先检查手机号码是否存在
        UserDO userDbDo = userMapper.existMobile(mobile);
        if (userDbDo != null) {
            return setResultError("该手机号码已经存在");
        }
        // dto转换成vo
        UserDO userDo = dtoToDo(userReqRegisterDto, UserDO.class);
        String newPassword = MD5Util.MD5(password + "salt");
        // 加盐 提前定义一个常量 可以用一张表定义每个用户对应的盐值
        userDo.setPassword(newPassword);
        int register = userMapper.register(userDo);
//        return register > 0 ? setResultSuccess("注册成功") : setResultError("注册失败");
        return setResult(register, "注册成功", "注册失败");
    }
}
